package com.gexiaobao.pigeon.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.BaseSignListResponse
import com.gexiaobao.pigeon.ui.adapter.AdapterGroupName
import com.yanzhenjie.recyclerview.SwipeRecyclerView

/**
 * @Author      : hxw
 * @Date        : 2024/7/16 15:13
 * @Describe    :
 */
class RxSignPassGroupNameDialog(context: Context?, signStatus: MutableList<BaseSignListResponse.SignPigeonListBean.SignStatusBean>) :
    RxDialog(context!!) {

    private var onSureClickListener: OnSureClickListener? = null
    private var onCancelClickListener: OnCancelClickListener? = null

    private lateinit var ivItemCheckedAll: AppCompatImageView
    private lateinit var llAllChooseGroup: LinearLayout
    private lateinit var tvSignTotalMoney: AppCompatTextView
    private lateinit var tvDialogSignRing: AppCompatTextView
    private lateinit var tvSure: AppCompatTextView
    private lateinit var tvCancel: AppCompatTextView
    private lateinit var swipeRecyclerView: SwipeRecyclerView
    private lateinit var mAdapter: AdapterGroupName
    private var mSignStatus: MutableList<BaseSignListResponse.SignPigeonListBean.SignStatusBean> = arrayListOf()
    private var mList: MutableList<BaseSignListResponse.SignPigeonListBean.SignStatusBean> = arrayListOf()
    private var invertList: MutableList<BaseSignListResponse.SignPigeonListBean.SignStatusBean> = arrayListOf()

    init {
        this.mSignStatus = signStatus
        initView()
    }

    @SuppressLint("MissingInflatedId", "NotifyDataSetChanged", "SetTextI18n")
    private fun initView() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_sign_pass_group, null)
        tvSure = dialogView.findViewById(R.id.tvDialogSure)
        tvCancel = dialogView.findViewById(R.id.tvDialogCancel)
        swipeRecyclerView = dialogView.findViewById(R.id.swipeRecyclerViewGroup)
        tvDialogSignRing = dialogView.findViewById(R.id.tvDialogSignRing)
        tvSignTotalMoney = dialogView.findViewById(R.id.tvSignTotalMoney)
        llAllChooseGroup = dialogView.findViewById(R.id.llAllChooseGroup)
        ivItemCheckedAll = dialogView.findViewById(R.id.ivItemCheckedAll)

        mAdapter = AdapterGroupName()
        swipeRecyclerView.layoutManager = LinearLayoutManager(context)
        swipeRecyclerView.adapter = mAdapter
        mAdapter.setList(mSignStatus)
        mAdapter.notifyDataSetChanged()

        var sum = 0.0
        mAdapter.data.forEach {
            if (it.isCheck) {
                sum += it.money.toDouble()
            }
        }
        tvSignTotalMoney.text = "合计: $sum"

        mAdapter.setOnMultipleChoiceClickListener(object : AdapterGroupName.OnGroupNameCheckClickListener {
            @SuppressLint("SetTextI18n")
            override fun onItemClick(
                position: Int,
                checkList: ArrayList<BaseSignListResponse.SignPigeonListBean.SignStatusBean>,
                list: List<BaseSignListResponse.SignPigeonListBean.SignStatusBean>,
                totalMomey: String
            ) {
                mList.clear()
                mList.addAll(list)
                tvSignTotalMoney.text = "合计: $totalMomey"
                if (checkList.size == mSignStatus.size) {
                    ivItemCheckedAll.setImageResource(R.mipmap.icon_sign_checked)
                } else {
                    ivItemCheckedAll.setImageResource(R.mipmap.icon_sign_unchecked)
                }
            }
        })

        tvCancel.setOnClickListener {
            onCancelClickListener?.onItemClick(mList)
        }

        tvSure.setOnClickListener {
            onSureClickListener?.onItemClick(mList)
        }

        llAllChooseGroup.setOnClickListener {
            chooseAllItem()
        }
        setContentView(dialogView)
    }

    @SuppressLint("SetTextI18n")
    private fun chooseAllItem() {
        if (invertList.size == mSignStatus.size) {
            //清除选中的条目
            invertList.clear()
            mAdapter.setClear()
            ivItemCheckedAll.setImageResource(R.mipmap.icon_sign_unchecked)
        } else {
            /** 获取全选之后的list数据 */
            invertList = mAdapter.setInvertSelection() as ArrayList<BaseSignListResponse.SignPigeonListBean.SignStatusBean>
            /** 新的；list保存全选的数据 是否全部选中 */
            mList.clear()
            mList.addAll(invertList)
            ivItemCheckedAll.setImageResource(R.mipmap.icon_sign_checked)
        }
        var sum = 0.0
        invertList.forEach {
            sum += it.money.toDouble()
        }
        tvSignTotalMoney.text = "合计: $sum"
    }

    fun setTitle(title: String) {
        if (title.isEmpty()) {
            tvDialogSignRing.visibility = View.GONE
            return
        }
        tvDialogSignRing.text = title
        tvDialogSignRing.visibility = View.VISIBLE
    }

    fun setOnSureClickListener(listener: OnSureClickListener?) {
        onSureClickListener = listener
    }

    interface OnSureClickListener {
        fun onItemClick(list: MutableList<BaseSignListResponse.SignPigeonListBean.SignStatusBean>)
    }

    fun setOnCancelClickListener(listener: OnCancelClickListener?) {
        onCancelClickListener = listener
    }

    interface OnCancelClickListener {
        fun onItemClick(list: MutableList<BaseSignListResponse.SignPigeonListBean.SignStatusBean>)
    }
}